﻿Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data.SqlClient

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class GetDefault
    Inherits System.Web.Services.WebService
    Dim connect As New Connection()
#Region "Default Infor"
    '- Goi ws de lay default vendor/customer
    '- default date = today
    '- Sau khi chon Item: hien thi ItemCode, Description, Quantity=1,  goi ws de lay gia tri default
    '- Doi so luong: goi ws de lay gia tri default
#End Region

    <WebMethod()> _
    Public Function GetDefaultLineInfo(ByVal dbType As String, ByVal ID_Dealer As String, ByVal cardCode As String, ByVal itemCode As String, _
                                       ByVal Quantity As Double, ByVal refDate As Date, ByVal ID_Department As String) As DataSet
        Dim a As New SAP_Functions
        Return a.GetDefaultLineInfo(dbType, ID_Dealer, cardCode, itemCode, Quantity, refDate, ID_Department)
    End Function
    <WebMethod()> _
    Public Function GetPartsDefaultLineInfo(ByVal dbType As String, ByVal ID_Dealer As String, ByVal cardCode As String, ByVal itemCode As String, _
                                       ByVal Quantity As Double, ByVal refDate As Date, ByVal ID_Department As String, ByVal priceType As String) As DataSet
        Dim a As New SAP_Functions
        Return a.GetPartsDefaultLineInfo(dbType, ID_Dealer, cardCode, itemCode, Quantity, refDate, ID_Department, priceType)
    End Function

    <WebMethod()> _
    Public Function GetDocumentList(ByVal dbType As String, ByVal ID_Dealer As String, ByVal CardCode As String, ByVal DocType As String, ByVal FromDockNum As String, _
                                     ByVal ToDockNum As String, ByVal FromDocDate As Date, ByVal ToDocDate As Date, ByVal FromDueDate As Date, ByVal ToDueDate As Date, _
                                    ByVal FromDoctotal As String, ByVal ToDoctotal As String, ByVal Status As String, ByVal department As String, _
                                    ByVal soNBR As String, ByVal debitNode As String) As DataSet
        Dim ds As DataSet
        Dim connect As New Connection()
        connect.setDB(dbType)

        Dim str As String
        Dim condition As String = String.Empty

        str = "select DocEntry AS DocNum, DocDate, DocDueDate, CardCode, CardName, Comments, DocTotal, DocStatus,U_SOType from "
        Select Case DocType
            Case "18"
                str = str + "OPCH"
            Case "19"
                str = str + "ORPC"
            Case "20"
                str = str + "OPDN"
            Case "21"
                str = str + "ORPD"
            Case "22"
                str = str + "OPOR"

            Case "13"
                str = str + "OINV"
            Case "14"
                str = str + "ORIN"
            Case "15"
                str = str + "ODLN"

            Case "17"
                str = str + "ORDR"

            Case "23"
                str = str + "OQUT"

            Case "24"
                str = "select  DocEntry AS DocNum, DocDate, DocDueDate, CardCode, CardName, Comments, DocTotal, '' DocStatus from ORCT"

            Case "46"
                str = "select  DocEntry AS DocNum, DocDate, DocDueDate, CardCode, CardName, Comments, DocTotal, '' DocStatus from OVPM"

            Case "59"
                str = str + "OIGN"
            Case "60"
                str = str + "OIGE"

            Case "67"
                str = str + "OWTR"

            Case "1250000001"
                str = str + "OWTQ"

            Case "97"
                str = str + "OOPR"

            Case "112"
                str = str + "ODRF"

        End Select

        ' Build condition to query
        condition = condition + " where convert(varchar(8), DocDate, 112) >= " + FromDocDate.ToString("yyyyMMdd")
        condition = condition + "   and convert(varchar(8), DocDate, 112) <= " + ToDocDate.ToString("yyyyMMdd")
        condition = condition + "   and convert(varchar(8), DocDueDate, 112) >= " + FromDueDate.ToString("yyyyMMdd")
        condition = condition + "   and convert(varchar(8), DocDueDate, 112) <= " + ToDueDate.ToString("yyyyMMdd")

        If String.IsNullOrEmpty(Status) = False Then
            condition = condition + "   and DocStatus = '" + Status + "'"
        End If

        If (CardCode.Length > 0) Then
            condition = condition + "   and CardCode = '" + CardCode + "'"
        End If

        If DocType = "112" Then 'Only get AP Draft
            condition = condition + "   and ObjType = 18 "
        End If

        ' Doc Entry condition
        If (String.IsNullOrEmpty(FromDockNum) = False And String.IsNullOrEmpty(ToDockNum) = False) Then
            condition = condition + "   and DocNum >= " + FromDockNum + "   and DocNum <= " + ToDockNum
        ElseIf (String.IsNullOrEmpty(FromDockNum) = False) Then
            condition = condition + "   and DocNum >= " + FromDockNum
        ElseIf (String.IsNullOrEmpty(ToDockNum) = False) Then
            condition = condition + "   and DocNum <= " + ToDockNum
        End If

        ' DocTotal condition
        If (String.IsNullOrEmpty(FromDoctotal) = False And String.IsNullOrEmpty(ToDoctotal) = False) Then
            condition = condition + "   and DocTotal >= " + FromDoctotal + "   and DocTotal <= " + ToDoctotal
        ElseIf (String.IsNullOrEmpty(FromDoctotal) = False) Then
            condition = condition + "   and DocTotal >= " + FromDoctotal
        ElseIf (String.IsNullOrEmpty(ToDoctotal) = False) Then
            condition = condition + "   and DocTotal <= " + ToDoctotal
        End If

        'Add by Hieu 21/5/2014 - START
        If String.IsNullOrEmpty(department) = False Then
            condition = condition + " and U_Department = '" & department & "'"
        End If
        'Add by Hieu 21/5/2014 - END
        If String.IsNullOrEmpty(ID_Dealer) = False Then
            condition = condition + " and U_DealerID = '" & ID_Dealer & "'"
        End If

        'Add by Hieu 26/6/2014 - START
        If String.IsNullOrEmpty(soNBR) = False Then
            condition = condition + " and U_SONBR LIKE '%" & soNBR & "%'"
        End If
        If String.IsNullOrEmpty(debitNode) = False Then
            condition = condition + " and U_DebitNote LIKE '%" & debitNode & "%'"
        End If
        'Add by Hieu 26/6/2014 - END

        str = str + condition
        ds = connect.ObjectGetAll_Query(str, dbType)
        Return ds

    End Function

    <WebMethod()> _
    Public Function GetIncomingPaymentList(ByVal dbType As String, ByVal ID_Dealer As String, ByVal CardCode As String, ByVal DocType As String, ByVal FromDockNum As String, _
                                     ByVal ToDockNum As String, ByVal FromDocDate As Date, ByVal ToDocDate As Date, ByVal FromDueDate As Date, ByVal ToDueDate As Date, _
                                    ByVal FromDoctotal As String, ByVal ToDoctotal As String, ByVal Status As String, ByVal PayNoDoc As String, ByVal department As String) As DataSet
        Dim ds As DataSet
        Dim connect As New Connection()
        connect.setDB(dbType)

        Dim str As String
        Dim condition As String = String.Empty

        str = "select DocNum, DocDate, DocDueDate, CardCode, CardName, Comments, DocTotal, '' DocStatus from ORCT"

        ' Build condition to query
        condition = " where PayNoDoc = '" + PayNoDoc + "'"
        condition = condition + " and convert(varchar(8), DocDate, 112) >= " + FromDocDate.ToString("yyyyMMdd")
        condition = condition + " and convert(varchar(8), DocDate, 112) <= " + ToDocDate.ToString("yyyyMMdd")
        condition = condition + " and convert(varchar(8), DocDueDate, 112) >= " + FromDueDate.ToString("yyyyMMdd")
        condition = condition + " and convert(varchar(8), DocDueDate, 112) <= " + ToDueDate.ToString("yyyyMMdd")

        If (Status.Length > 0) Then
            condition = condition + "   and DocStatus = '" + Status + "'"
        End If

        If (CardCode.Length > 0) Then
            condition = condition + "   and CardCode = '" + CardCode + "'"
        End If

        ' Doc Entry condition
        If (String.IsNullOrEmpty(FromDockNum) = False And String.IsNullOrEmpty(ToDockNum) = False) Then
            condition = condition + "   and DocNum >= " + FromDockNum + "   and DocNum <= " + ToDockNum
        ElseIf (String.IsNullOrEmpty(FromDockNum) = False) Then
            condition = condition + "   and DocNum >= " + FromDockNum
        ElseIf (String.IsNullOrEmpty(ToDockNum) = False) Then
            condition = condition + "   and DocNum <= " + ToDockNum
        End If

        ' DocTotal condition
        If (String.IsNullOrEmpty(FromDoctotal) = False And String.IsNullOrEmpty(ToDoctotal) = False) Then
            condition = condition + "   and DocTotal >= " + FromDoctotal + "   and DocTotal <= " + ToDoctotal
        ElseIf (String.IsNullOrEmpty(FromDoctotal) = False) Then
            condition = condition + "   and DocTotal >= " + FromDoctotal
        ElseIf (String.IsNullOrEmpty(ToDoctotal) = False) Then
            condition = condition + "   and DocTotal <= " + ToDoctotal
        End If

        If String.IsNullOrEmpty(department) = False Then
            condition = condition + " and U_Department = '" & department & "'"
        End If

        If String.IsNullOrEmpty(ID_Dealer) = False Then
            condition = condition + " and U_DealerID = '" & ID_Dealer & "'"
        End If

        str = str + condition

        ds = connect.ObjectGetAll_Query(str, dbType)
        Return ds

    End Function

    <WebMethod()> _
    Public Function GetForecastsList(ByVal dbType As String, ByVal ID_Dealer As String, ByVal Code As String, _
                                     ByVal FromStartDate As Date, ByVal ToStartDate As Date, ByVal FromEndDate As Date, ByVal ToEndDate As Date) As DataSet
        Dim ds As DataSet
        Dim connect As New Connection()
        connect.setDB(dbType)

        Dim str As String
        Dim condition As String = String.Empty

        str = "select AbsID, Code, Name, StartDate, EndDate from OFCT"

        ' Build condition to query
        condition = condition + " where convert(varchar(8), StartDate, 112) >= " + FromStartDate.ToString("yyyyMMdd")
        condition = condition + "   and convert(varchar(8), StartDate, 112) <= " + ToStartDate.ToString("yyyyMMdd")
        condition = condition + "   and convert(varchar(8), EndDate, 112) >= " + FromEndDate.ToString("yyyyMMdd")
        condition = condition + "   and convert(varchar(8), EndDate, 112) <= " + ToEndDate.ToString("yyyyMMdd")
        condition = condition + "   and U_DealerID ='" + ID_Dealer + "'"
        ' Doc Entry condition

        If (String.IsNullOrEmpty(Code) = False) Then
            condition = condition + "   and Code = " + Code
        End If

        str = str + condition

        ds = connect.ObjectGetAll_Query(str, dbType)
        Return ds

    End Function

    <WebMethod()> _
    Public Function CheckNumAtCard(ByVal dbType As String, ByVal ID_Dealer As String, ByVal DocType As String, ByVal RefNo As String) As Boolean
        Dim f As New SAP_Functions
        Dim connect As New Connection()
        connect.setDB(dbType)

        Dim str As String
        Dim condition As String = String.Empty, RetValue As String = String.Empty

        str = "select NumAtCard from "
        Select Case DocType
            Case "19"
                str = str + "ORPC"
            Case "20"
                str = str + "OPDN"
            Case "21"
                str = str + "ORPD"
            Case "22"
                str = str + "OPOR"

            Case "13"
                str = str + "OINV"
            Case "14"
                str = str + "ORIN"
            Case "15"
                str = str + "ODLN"

            Case "17"
                str = str + "ORDR"

            Case "23"
                str = str + "OQUT"

            Case "24"
                str = str + "ORCT"

            Case "59"
                str = str + "OIGN"
            Case "60"
                str = str + "OIGE"

            Case "67"
                str = str + "OWTR"

            Case "1250000001"
                str = str + "OWTQ"

            Case "97"
                str = str + "OOPR"
        End Select

        ' Build condition to query
        condition = " where NumAtCard = '" + RefNo + "'"

        str = str + condition

        RetValue = f.GetRefNo(dbType, str)
        If RetValue.Length > 0 Then
            Return True
        End If

        Return False

    End Function

    <WebMethod()> _
    Public Function GetMaxDocEntry(ByVal dbType As String, ByVal ID_Dealer As String, ByVal ID_Department As String, ByVal ObjType As String, ByVal TableName As String, ByVal KeyName As String) As String
        Dim f As New SAP_Functions
        'Dim connect As New Connection()
        'connect.setDB(dbType)
        Return f.GetMaxDocEntry(dbType, ID_Dealer, ID_Department, ObjType, TableName, KeyName)

    End Function

    <WebMethod()> _
    Public Function GetAccountNameByCode(ByVal dbType As String, ByVal ID_Dealer As String, ByVal AccountCode As String) As String
        Dim f As New SAP_Functions
        Dim connect As New Connection()
        connect.setDB(dbType)

        Return f.GetAccountName(dbType, "select AcctName from OACT where AcctCode = '" + AccountCode + "'")
        
    End Function

#Region "Apply Promotion"
    '- Neu WS GetPromotion tra ve nhieu hon 1 record thi show page "Promotion Selection" de chon 1, sau do theo logic duoi.

    '- Neu WS GetPromotion Tra ve 1 record thi theo logic duoi.

    'Logic Apply Promotion:
    '1. Discount By Promotion = HeadDscAmt + HeadDscPer*UnitPrice/100 + ProValue
    '2. Unit Price = Unit Price - Discount By Promotion
    '3. So le = field Sole
    '4. ProCode = field ProCode
    '5. Neu ProQty>0: them 1 dong vao grid
    '    Item Code= Item Code cua dong apply
    '    Description = Description cua dong apply
    '    Quantity=ProQty
    '    Unit Price,Discount,Discount By Promotion  = 0
    '    Warehouse = WS GetPromotionWarehouse
    '    U_ProLine = Y
    '    ProCode = field ProCode
    '    So le = field Sole
#End Region
  
    <WebMethod()> _
    Public Function GetCopyFromTo(Type As Integer, ObjType As String) As DataSet
        Dim a As New SAP_Functions
        'Type=1: Copy To
        'Type=2: Copy From
        'ObjType=22: Purchase Order
        Return a.GetGopyFromTo(Type, ObjType)
    End Function
End Class